/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.midnightcookies;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
public class FrontController extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

     String id = request.getRequestURI();
     id = id.substring(request.getContextPath().length());
     getServletContext().log
       ("Midnight Cookies FrontController received a request for " + id);

     // The page variable holds the path to the page that will be included
     // in the content area of the template page /WEB-INF/docs/main.jsp.
     // It is passed to the template in a request attribute.
     String page;

     // If no request is received or if the request received is
     // for the root, serve /WEB-INF/docs/index.jsp
     if (id == null || id.trim().equals("") || id.equals("/")){
         page = "/WEB-INF/docs/index.jsp";
     }

     // If a request received is for a file that does not end in
     // .jsp or .html, there was an error. Include the
     // error page (nonesuch.jsp) and set the nonesuch attribute with
     // the path requested.
     else if (!id.endsWith(".jsp") && !id.endsWith(".html")) {
         page = "/WEB-INF/docs/nonesuch.jsp";
         request.setAttribute("nonesuch", id);
     }
     else {
         page = "/WEB-INF".concat(id);

         if (id.equals("/docs/cookies/CookieMake.jsp")) {
             Cookie cookie = createCookie(request);
             if (cookie == null) {
                 page = "/WEB-INF/docs/cookies/CookieCutter.jsp";
             }
             else {
                 response.addCookie(cookie);
                 request.setAttribute("cookie", cookie);
             }
         }
     }

     request.setAttribute("page", page);
     try {
         request.getRequestDispatcher("/WEB-INF/docs/main.jsp").forward(request, response);
     }
     catch(Throwable t) {
         getServletContext().log(t.getMessage());
     }
}
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private Cookie createCookie(HttpServletRequest request) {

        String name = (String)request.getParameter("name");
        if (name == null || name.trim().equals("")) {
            request.setAttribute("noname", "noname");
            request.setAttribute("error", "true");
             return null;
        }

        String value = (String)request.getParameter("value");
        if (value == null || value.trim().equals("")) {
            request.setAttribute("novalue", "novalue");
            request.setAttribute("error", "true");
             return null;
        }

         System.out.println(name);
         System.out.println(value);

         Cookie cookie = null;
         try {
             cookie = new Cookie(name, value);
         }
         catch(IllegalArgumentException ex) {
             // Probably an illegal name
             ex.printStackTrace();
             request.setAttribute("error", "true");
             request.setAttribute("noname", "badname");
             return null;
         }

         String maxage = request.getParameter("maxage");
         if (maxage != null && !maxage.trim().equals("")) {
             try {
                 cookie.setMaxAge(Integer.parseInt(maxage));
             }
             catch(NumberFormatException nfe) {
                 nfe.printStackTrace();
                 request.setAttribute("badnumber", "badnumber");
                 request.setAttribute("error", "true");
                 return null;
             }
         }

         String domain = request.getParameter("domain");
         if (domain != null && !domain.trim().equals("")) {
             cookie.setDomain(domain);
         }

         String path = request.getParameter("path");
         if (path != null && !path.trim().equals("")) {
             cookie.setPath(path);
         }

         String secure = request.getParameter("secure");
         if (secure != null && secure.equals("on")) {
             cookie.setSecure(true);
         } else {
             cookie.setSecure(false);
         }
        return cookie;
}

}
